<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="../assets/avatar.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="80px"
        class="login-form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <!-- rel即表单的实例对象，:model是将数据传出去，v-model是绑定数据 -->
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="iconfont icon-denglu"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="iconfont icon-password"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="userAndPwd">用户名：admin<br>密码：123456</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //登录表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名长度为3~12个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "密码长度为6~18个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields(); //重置表单实例数据，并失焦
    },
    //valid为validate的返回值，通过验证则为true
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm); //login为API所给的请求地址,参数二为登录表单的数据绑定对象，此次post请求是一个promise，为了简化操作，利用await来进行优化，而await只能用在被async修饰的方法中。{ data: res }为解构赋值操作，data为真实需要返回的数据对象，重命名为res对象。
        if (res.meta.status !== 200) {
          return this.$message.error({ message: "登录失败！", center: true });
        }
        /* this.$message.success("登录成功！"); */
        this.$message.success({ message: "登录成功！", center: true });
        console.log(res);
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style  lang="less" scoped>
//lang="less"表示支持less语法格式。scoped用于控制指令的生效区间
.login-container {
  background-color: #eef7f2;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute; //定位前需要有绝对定位属性
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); //基于自身盒子进行移动
}
.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #f0f5e5;
  border-radius: 50%;
  padding: 4px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%); //基于自身盒子进行移动
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login-form {
  position: absolute;
  bottom: 0%;
  width: 86%;
}
.btns {
  display: flex;
  justify-content: center;
}
.userAndPwd{
  position: absolute;
  left: 1%;
  top: 88%;
  background: lightblue;
  width: 200px;
  height: 54px;
  border-radius: 10px;
  font-size: 18px;
  text-align: center;
}
</style>
